<template>
  <div>
    <h1>{{ id ? `编辑` : `新建` }}活动</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <!-- <el-form-item label="上级分类"> 
                <el-select v-model="model.parent" placeholder="请选择">
                  <el-option v-for="item in parent" :key='item._id' :label='item.name' :value='item._id'></el-option>
                </el-select>
            </el-form-item>  -->
      <!-- 默认显示是哪一个选项卡 -->
      <el-tabs value="basic" type="card">
        <el-tab-pane label="基本信息" name="basic">
          <el-form-item label="活动归属" width="120">
            <el-select v-model="model.categories">
              <el-option
                v-for="item in categories"
                :label="item.name"
                :value="item._id"
                :key="item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="活动名称">
            <el-input v-model="model.name"/>
          </el-form-item>
          
          <el-form-item label="活动人数" width="120">
            <el-input v-model="model.personNum"/>
          </el-form-item>
          <el-form-item label="活动金额" width="120">
            <el-input v-model="model.moneyNum"/>
          </el-form-item>
          <el-form-item label="活动时间" width="120">
            <el-input v-model="model.time"/>
          </el-form-item>
          <el-form-item label="封面图片">
            <el-upload
              class="avatar-uploader"
              :action="uploadURL"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="(res) => $set(model, 'image', res.url)"
            >
              <!-- 有图片显示图片，没有则显示上传图标,:src显示的图片 -->
              <img v-if="model.image" :src="model.image" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="小标题">
            <el-input type="tell" v-model="model.tell" />
          </el-form-item>
          <el-form-item label="描述一">
            <el-input type="textarea" v-model="model.nr1" />
          </el-form-item>
          <el-form-item label="描述二">
            <el-input type="textarea" v-model="model.nr2" />
          </el-form-item>
        </el-tab-pane>

        <el-tab-pane label="细节图片" name="detailImg">
          <el-button size="small" @click="model.detailImg.push({})">
            <i class="el-icon-plus"></i>添加图片</el-button
          >
          <el-row type="flex">
            <el-row>
              <!-- :md='12'表示普通屏幕一行显示两个框 -->
              <el-col
                :md="24"
                v-for="(item, i) in model.detailImg"
                v-bind:key="i"
              >
                <el-form-item label="图片名称">
                  <el-input v-model="item.imgName" />
                </el-form-item>
                <el-form-item label="细节图片">
                  <el-upload
                    class="avatar-uploader"
                    :action="uploadURL"
                    :headers="getAuthHeaders()"
                    :show-file-list="false"
                    :on-success="(res) => $set(item, 'img', res.url)"
                  >
                    <!-- 有图片显示图片，没有则显示上传图标,:src显示的图片 -->
                    <img v-if="item.img" :src="item.img" class="avatar" />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </el-form-item>
                <el-form-item>
                  <el-button
                    size="small"
                    type="danger"
                    @click="model.skills.splice(i, 1)"
                  >
                    删除
                  </el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top: 1rem">
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        name: "",
        // avatar:'',
        // scores:{diffcult:0},
        detailImg: [],
        // partners:[],
      },
      categories: [],
      // items:[],
      // heroes:[]
    };
  },
  methods: {
    async save() {
      let res;
      if (this.id) {
        res = await this.$http.put(`rest/Activity/${this.id}`, this.model);
      } else {
        res = await this.$http.post("rest/Activity", this.model);
      }
      this.$router.push("/activity/list");
      this.$message({
        type: "success",
        message: "保存成功",
      });
    },
    async fetch() {
      const res = await this.$http.get("rest/Activity/" + this.id);
      this.model = Object.assign({}, this.model, res.data);
      // 这样写也行
      // this.model = {...this.model,...res.data}
    },
    async fetchCategories() {
      const res = await this.$http.get("rest/Categories");
      // console.log(res);
      this.categories = res.data;
    },

    //  async fetchActivities(){
    //     const res = await this.$http.get('rest/Activity')
    //     this.heroes = res.data
    //  },
  },
  created() {
    this.fetchCategories();
    // this.fetchActivities()
    this.id && this.fetch();
    // 等价于
    // if(this.id){
    //     this.fetch()
    // }
  },
};
</script>



<style scoped>
</style>